// pages/user/company/companyProperty/companyProperty.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: null,
        time: null,
        userInfo: null,
        recordList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.time) {
            this.setData({
                type: options.type,
                time: options.time
            })
        } else {
            this.setData({
                type: options.type
            })
        }
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            recordList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getRecordList()
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getRecordList()
            }
        }).catch(function (e) {})
    },

    getRecordList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let recordList = this.data.recordList
        let data
        if (this.data.time == null) {
            if (this.data.type == 'manager') {
                data = {
                    people_type: 1,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.type == 'filiale') {
                data = {
                    filiale_id: this.data.userInfo.filiale_id,
                    people_type: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.type == 'shop') {
                data = {
                    store_id: this.data.userInfo.store_id,
                    people_type: 3,
                    limit: 10,
                    page: this.data.nowPage
                }
            }
        } else {
            if (this.data.type == 'manager') {
                data = {
                    time: this.data.time,
                    people_type: 1,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.type == 'filiale') {
                data = {
                    time: this.data.time,
                    filiale_id: this.data.userInfo.filiale_id,
                    people_type: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.type == 'shop') {
                data = {
                    time: this.data.time,
                    store_id: this.data.userInfo.store_id,
                    people_type: 3,
                    limit: 10,
                    page: this.data.nowPage
                }
            }
        }
        common.post('Data/property_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    recordList: recordList,
                    isAll: true
                })
            } else {
                recordList = recordList.concat(res)
                that.setData({
                    recordList: recordList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) {})
    },

    goOrderDetail: function (e) {
        wx.navigateTo({
            url: '/pages/order/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id + '&dignity=admin'
        })
    }
})